package ua.maxicash.editors;


import ua.maxicash.service.GeoService;
import ua.maxicash.vo.City;

import java.beans.PropertyEditorSupport;

//@Component("cityEditor")
public class CityEditor extends PropertyEditorSupport {

    //@Autowired
    GeoService geoService;

    public CityEditor(GeoService geoService) {
        this.geoService = geoService;
    }

    @Override
    public String getAsText() {

        City city = (City) getValue();
        if (city != null) {
            return city.getIdCity().toString();
        } else {
            return "";
        }
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {

        City city = geoService.getCityById(Long.valueOf(text));
        if (city != null) {
            setValue(city);
        } else {
            throw new IllegalArgumentException();
        }

    }
}
